<template>
    <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

         <el-form-item label="昵称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>

        <el-form-item label="姓名" prop="realName">
            <el-input v-model="ruleForm.realName"></el-input>
        </el-form-item>

        <el-form-item label="积分" prop="scope">
            <el-input v-model="ruleForm.scope"></el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" show-password></el-input>
        </el-form-item>

        <el-form-item label="专业" prop="college">
            <el-input v-model="ruleForm.college" ></el-input>
        </el-form-item>


        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>

    </el-form>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    name: '',
                    realName:'',
                    scope:'',
                    phone:'',
                    password:'',
                    college: ''
                },
                rules: {
                   name: [
                        { required: true, message: '昵称不能为空', trigger: 'blur' }
                    ],realName: [
                        { required: true, message: '姓名不能为空', trigger: 'blur' }
                    ],phone: [
                        { required: true, message: '联系方式不能为空', trigger: 'blur' }
                    ],password: [
                        { required: true, message: '密码不能为空', trigger: 'blur' }
                    ],
                    college:[
                        { required: true, message: '专业不能为空', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                const _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.put('http://localhost:8181/book/update',this.ruleForm).then(function(resp){
                            if(resp.data == 'success'){
                                _this.$alert('用户“'+_this.ruleForm.name+'”修改成功！', '消息', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        _this.$router.push('/BookManage')
                                    }
                                })
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        created() {
            const _this = this
            axios.get('http://localhost:8181/book/findById/'+this.$route.query.id).then(function(resp){
                _this.ruleForm = resp.data
            })
        }
    }
</script>